<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--搜索区域-->
      <!--操作按钮区域-->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleImport"
            v-hasPermi="['system:user:add']"
          >导入
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                     v-hasPermi="['course:subject:export']">导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>
      <!--数据列表区域-->
      <el-table v-if="refreshTable" v-loading="loading" :data="subjectList" row-key="id"
                :default-expand-all="isExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column label="编号" align="center" key="id" prop="id"/>
        <el-table-column label="名称" align="center" key="title" prop="title"/>
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" v-hasPermi="['course:trainer:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" v-hasPermi="['course:trainer:remove']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

    </el-row>

    <!-- 课程分类导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的分类数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="downloadTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {listAll} from "@/api/course/subject";
import {handleTree} from "@/utils/pp";
import {getToken} from "@/utils/auth";

export default {
  // 定义数据模型
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      subjectList: null,
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        currentPage: 1,
        pageSize: 10,
        name: undefined,
        level: undefined
      },
      // 表单参数
      form: {},
      // 弹出层标题
      title: "",
      // 表单校验
      rules: {
        name: [
          {required: true, message: "培训师名称不能为空", trigger: "blur"},
          {min: 2, max: 8, message: '培训师名称长度必须介于 2 和 8 之间', trigger: 'blur'}
        ],
        level: [
          {required: true, message: "培训师头衔不能为空", trigger: "blur"},
        ],
      },
      // 重新渲染表格状态
      refreshTable: true,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 分类数据导入
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/subject/import"
      }
    }
  },
  // 钩子函数
  created() {
    // 初始化页面数据
    this.getList();
  },
  // 自定义函数
  methods: {
    getList() {
      this.loading = true;
      listAll().then(response => {
        // 需要将所有的数据构建成树形的数据
        this.subjectList = handleTree(response.data, "id")
        this.loading = false;
      })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "课程分类数据导入";
      this.upload.open = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('subject/export', {...this.queryParams}, `subject_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.message + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    downloadTemplate() {
      this.download('/subject/downloadTemplate', {}, `课程分类模板_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
